package com.example.kaiqiang.dagger2demo

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import kotlinx.android.synthetic.main.main_activity.*

class MainActivity : AppCompatActivity() {
    lateinit var mFragments: MutableList<Fragment>
    private var menuItem: MenuItem? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        mFragments = ArrayList()
        mFragments.add(MyFragment.newInstance(0, "妹子"))
        mFragments.add(MyFragment.newInstance(1, "Android"))

        mFragments.add(MyFragment.newInstance(2, "前端"))


        viewpager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return mFragments[position]
            }

            override fun getCount() = mFragments.size

        }
        viewpager.offscreenPageLimit = 3
        navigationView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.item_news -> viewpager.currentItem = 0
                R.id.item_lib -> viewpager.currentItem = 1
                R.id.item_find -> viewpager.currentItem = 2
            }
            true
        }
        viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {


            }

            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageSelected(position: Int) {
                if (menuItem != null) {
                    menuItem!!.isChecked = false
                } else {
                    navigationView.menu.getItem(0).isChecked = false
                }
                menuItem = navigationView.menu.getItem(position)
                menuItem!!.isChecked = true




            }

        })


    }
}